<?php

class WPSPO_Promesse extends WPSPO_Object {
    
    var $errors = array();
    
    function save($defi, $projet, $user, $parent= '', $texte, $montant, $type = 1, $total = 0) {
        global $wpdb;
        //if($type==2) { $total = $montant; } else { $total = 0; }
        $data = array(
            'promesse_id_defi' => $defi,
            'promesse_id_projet' => $projet,
            'promesse_texte' => $texte,
            'promesse_id_user' => $user,
            'promesse_id_parent' => $parent,
            'promesse_montant' => $montant,
            'promesse_total' => $total  
        );
        $result = $wpdb->insert(WSPO_PROMESSE, $data);
        if($result) {
            return true;
        }
    }
    
    function get_id($id) {
        global $wpdb;
        $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM ". WSPO_PROMESSE . " WHERE promesse_id = %d LIMIT 1", $id), 'OBJECT' );
        if($result) {
            return $result;
        } 
    }
    
    function get_byUser($idUser, $id) {
        global $wpdb;
        $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM ". WSPO_PROMESSE . " WHERE promesse_id_defi = %d AND promesse_id_user = %d AND promesse_status='1' LIMIT 1", $id, $idUser), 'OBJECT' );
        if($result) {
            return $result;
        } 
    }
    
    function get_byIdDefi($id) {
        global $wpdb;
        $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM ". WSPO_PROMESSE . " WHERE promesse_id_defi = %d AND promesse_id_parent='' ORDER BY promesse_id DESC", $id), 'OBJECT' );
        if($result) {
            return $result;
        } 
    }
    
    function get_byIdProject($id) {
        global $wpdb;
        $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM ". WSPO_PROMESSE . " WHERE promesse_id_projet = %d ORDER BY promesse_id DESC", $id), 'OBJECT' );
        if($result) {
            return $result;
        } 
    }
    
    function get_parentByIdDefi($id, $idParent) {
        global $wpdb;
        $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM ". WSPO_PROMESSE . " WHERE promesse_id_defi = %d AND promesse_id_parent = %d ", $id, $idParent), 'OBJECT' );
        
        if($result) {
            return $result;
        } 
    }
    function get_countParentByIdDefi($id, $idParent) {
        global $wpdb;
        $result = $wpdb->get_var( $wpdb->prepare( "SELECT count(promesse_id) FROM ". WSPO_PROMESSE . " WHERE promesse_id_defi = %d AND promesse_id_parent = %d ", $id, $idParent ) );
        
        if($result) {
            return $result;
        } 
    }
    
    function get_countByUser($iduser, $idParent, $idDefi) {
        global $wpdb;
        $result = $wpdb->get_var( $wpdb->prepare( "SELECT count(promesse_id) FROM ". WSPO_PROMESSE . " WHERE promesse_id_user = %d AND promesse_id_parent = %d AND promesse_id_defi = %d ", $iduser, $idParent, $idDefi ) );
        
        if($result) {
            return $result;
        } 
    }
    
    function updateMontant($idDefi, $idProm, $iduser, $montant) {
        global $wpdb;
        $data = array(
            'promesse_total' => $montant,
            'promesse_valide' => 2
        );
        $where = array( 'promesse_id_defi' => $idDefi, 'promesse_id' => $idProm, 'promesse_id_user' => $iduser);
        $result = $wpdb->update(WSPO_PROMESSE, $data, $where);
        if($result) {
            return true;
        }      
    }
    
    function updateById($idProm, $montant, $texte = '', $total = '') {
        global $wpdb;
        $data = array(
            'promesse_montant' => $montant,
            'promesse_texte' => $texte,
            'promesse_total' => $total
        );
        $where = array( 'promesse_id' => $idProm);
        $result = $wpdb->update(WSPO_PROMESSE, $data, $where);
        if($result) {
            return true;
        }      
    }
    function update_totalById($idProm, $total) {
        global $wpdb;
        $data = array(          
            'promesse_total' => $total,
            'promesse_status' => 2
        );
        $where = array( 'promesse_id' => $idProm);
        $result = $wpdb->update(WSPO_PROMESSE, $data, $where);
        if($result) {
            return true;
        }      
    }
    function validate($idUser, $idProm) {
        global $wpdb;
        $data = array(
            'promesse_valide' => 1,
            'promesse_statut' => 2
        );
        $where = array( 'promesse_id_user' => $idUser, 'promesse_id' => $idProm);
        $result = $wpdb->update(WSPO_PROMESSE, $data, $where);
        if($result) {
            return true;
        }      
    }
    
    function get_sum($id, $type = '', $cpt = 'projet') {
        global $wpdb;
        if($type==1) { $sum = 'promesse_montant'; } else { $sum = 'promesse_total'; } 
        if($cpt == 'projet') {
            $result = $wpdb->get_var( $wpdb->prepare( "SELECT sum(".$sum.") FROM ". WSPO_PROMESSE . " WHERE promesse_status='1' AND promesse_id_projet = %d", $id ) );
        } else {
            $result = $wpdb->get_var( $wpdb->prepare( "SELECT sum(".$sum.") FROM ". WSPO_PROMESSE . " WHERE promesse_status='1' AND promesse_id_defi = %d", $id ) );
        }
        if($result) {
            return $result;
        } else {
            return 0;
        }
    }
    
    function get_sumByUser($id, $idUser) {
        global $wpdb;
        $result = $wpdb->get_var( $wpdb->prepare( "SELECT sum(promesse_montant) FROM ". WSPO_PROMESSE . " WHERE promesse_status='1' AND promesse_id_defi = %d AND promesse_id_parent = %d", $id, $idUser ) );
        if($result) {
            return $result;
        }
    }
    
    function listByUser($idUser) {
        global $wpdb;
        
        if(!$idUser) { die('Aucun utilisateur sélectionné'); }
        
        $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM ". WSPO_PROMESSE . " WHERE (promesse_status='1' OR promesse_status='2') AND promesse_id_user = %d ", $idUser), 'OBJECT' );
        if($result) {
            return $result;
        } 
    }
    
    function listByUserByDefi($idUser, $iddefi) {
        global $wpdb;
        
        if(!$idUser or !$iddefi) { die('Aucun utilisateur/défi sélectionné !'); }
        
        $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM ". WSPO_PROMESSE . " WHERE promesse_status='1' AND promesse_id_defi = %d AND promesse_id_user = %d", $iddefi, $idUser), 'OBJECT' );
        if($result) {
            return $result;
        } 
    }
    
    function listByDefi($iddefi) {
        global $wpdb;
        
        $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM ". WSPO_PROMESSE . " WHERE promesse_status='1' AND promesse_id_defi = %d" , $iddefi), 'OBJECT' );
        if($result) {
            return $result;
        } 
    }
    
    function get_list($iddefi = '') {
        global $wpdb;
        if($iddefi) {
            $add = "WHERE promesse_status='1' AND promesse_id_defi = %d";
        }
        $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM ". WSPO_PROMESSE . "".$add."", $iddefi), 'OBJECT' );
        if($result) {
            return $result;
        } 
    }
    
    /*
     * Compte le nombre de participant pour l'user sur un projet spécifique
     */
    function get_countByDefi($idDefi, $parent = 0) {
        global $wpdb;
        if($parent==1) {
            $addSql = "AND promesse_id_parent = '0'";
        }
        $promesse_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM ".WSPO_PROMESSE." WHERE promesse_id_defi = %d $addSql ", $idDefi ) );
        return $promesse_count;
    }
    /*
     * Compte le nombre de paticipant sur un projet spécifique
     */
    function get_countByProjet($idProjet) {
        global $wpdb;
        $promesse_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM ".WSPO_PROMESSE." WHERE promesse_id_projet = %d " , $idProjet ) );
        return $promesse_count;
    }
    
    /*
     * Upadate status des promesses
     */
    function updateStatus($idDefi, $status) {
        global $wpdb;
        $result = $wpdb->get_results( $wpdb->prepare( "UPDATE ". WSPO_PROMESSE . " SET promesse_status = %d WHERE promesse_id_defi = %d", $status, $idDefi), 'OBJECT' );
        if($result) {
            return true;
        } 
    }
    
    /*
     * Supprime une promesse en particulier
     */
    function delete($user, $idProm) {
		global $wpdb;
        // Supprime dans la table des promesses 'PREFIX_wpspo_promesse'
		$resultOptions =  $wpdb->query( $wpdb->prepare("DELETE FROM ". WSPO_PROMESSE . " WHERE promesse_id = %d AND promesse_id_user = %d LIMIT 1", $idProm, $user), 'OBJECT' );
        //$resultOptions = $wpdb->query( $deleteOptions );
        //$resultOptions = $wpdb->query( "DELETE FROM ". WSPO_PROMESSE . " WHERE promesse_id=".$idProm." AND promesse_id_user=".$user." LIMIT 1" );
        
		if($resultOptions) {
            return true;
        }
	}
    
    /*
     * Supprime les promesses d'un défi
     */
    function deleteByDefi($id) {
		global $wpdb;
        // Supprime dans la table des promesses 'PREFIX_wpspo_promesse'
		$resultOptions =  $wpdb->query( $wpdb->prepare("DELETE FROM ". WSPO_PROMESSE . " WHERE promesse_id_defi = %d", $idProm, $user), 'OBJECT' );        
		if($resultOptions) {
            return true;
        }
	}
}